﻿using Sundry.DAL;
using Sundry.IDAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Sundry.BLL
{
    public abstract class BaseService<T> where T : class,new()
    {
        /// <summary>
        /// 当前仓储
        /// </summary>
        public IDAL.IBaseRepository<T> CurrentRepository { get; set; }
        /// <summary>
        /// 用来实现获取所需仓储
        /// </summary>
        public abstract void SetCurrentRepository();//约束子类必须实现该抽象方法
        public BaseService()
        {
            SetCurrentRepository();//用来实现获取所需仓储
        }

        public IDbSession _dbSession = DbSessionFactory.GetCurrentDbSession();
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int AddEntity(T entity)
        {
            var addEntity = CurrentRepository.AddEntity(entity);
            return _dbSession.SaveChange();
        }
        /// <summary>
        /// 修改一条记录
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="property">需要修改的字段名称</param>
        /// <returns></returns>
        public bool EditEntity(T entity,string[] propery)
        {
            var editEntity = CurrentRepository.EditEntity(entity, propery);
            return _dbSession.SaveChange()>0;
        }
        /// <summary>
        /// 删除一条记录
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool DeleteEntity(T entity)
        {
            var deleteEntity = CurrentRepository.DeleteEntity(entity);
            return _dbSession.SaveChange() > 0;
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <returns></returns>
        public IQueryable<T> LoadEntities()
        {
            return CurrentRepository.LoadEntities();
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="whereLamda">查询条件</param>
        /// <returns></returns>
        public IQueryable<T> LoadEntities(Expression<Func<T, bool>> whereLamda)
        {
            return CurrentRepository.LoadEntities(whereLamda);
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <typeparam name="S">排序类型</typeparam>
        /// <param name="whereLamda">查询条件</param>
        /// <param name="orderLamda">排序条件</param>
        /// <param name="isDesc">是否倒序</param>
        /// <returns></returns>
        public IQueryable<T> LoadEntities<S>(Expression<Func<T, bool>> whereLamda, Expression<Func<T, S>> orderLamda, bool isDesc)
        {
            return CurrentRepository.LoadEntities(whereLamda, orderLamda, isDesc);
        }
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <typeparam name="S">排序类型</typeparam>
        /// <param name="whereLamda">查询条件</param>
        /// <param name="orderLamda">排序条件</param>
        /// <param name="isDesc">是否倒序</param>
        /// <param name="pageIndex">第几页</param>
        /// <param name="pageSize">页长</param>
        /// <param name="rowCount"></param>
        /// <returns></returns>
        public IQueryable<T> LoadEntities<S>(Expression<Func<T, bool>> whereLamda, Expression<Func<T, S>> orderLamda, bool isDesc, int pageIndex, int pageSize, out int rowCount)
        {
            return CurrentRepository.LoadEntities(whereLamda, orderLamda, isDesc, pageIndex, pageSize, out rowCount);
        }
    }
}
